package com.soft.cake.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.soft.cake.pojo.Img;
import com.soft.cake.pojo.Product;
import com.soft.cake.service.IImgService;
import com.soft.cake.service.IProductService;

/**
 * @author Yanyan Shi
 * @description use to controll the page of product
 */
@Controller
public class ProductController {
	
	@Autowired
	private IProductService productService;
	@Autowired
	private IImgService imgService;
	
	/**
	 * @description 商品详情按钮请求
	 * @author Yanyan Shi
	 * @param productId response model 
	 * @return error / single
	 */
	@RequestMapping("single/{productid}")
	public String details(@PathVariable("productid") Integer productId, HttpServletResponse response, Model model) {
		Product product = productService.detailsProduct(productId);
		List<Img> imgList = imgService.imgList(productId);
		if (product==null && imgList==null) {
			//TODO : add a page for error
			return "single";
		}
		model.addAttribute("product", product);
		model.addAttribute("imgList", imgList);
		System.out.println(imgList.get(0).getImgUrl());
		
		return "single";
		
	}
	
	/**
	 * @description 获取所有商品
	 * @author Huo Sa
	 * @param //TODO remember to note like this
	 * @return
	 */
	@RequestMapping("product/all")
	public String listProducts(Model model) {
		List<Product> products = productService.listProducts();
		model.addAttribute("products", products);
		return "products";
	}
	
	/**
	 * @description 获取单个商品
	 * @author Huo Sa
	 * @param //TODO remember to note like this
	 * @return
	 */
	@RequestMapping("product/{productId}")
	public String oneProduct(@PathVariable("productId") Integer productId,Model model) {
		
		Product product = productService.oneProduct(productId);
		Img img = imgService.selectByProductKey(productId);
		model.addAttribute("img", img);
		model.addAttribute("product", product);
		return "products";
	}	

//	@RequestMapping("index")
//	public List<Product> selectEightProducts() {
//
//		return products;
//	}
}
